﻿using CalendarWindowsApp.Tools;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using Windows.ApplicationModel.Appointments;

namespace CalendarWindowsApp.Controls.Calendar
{
    public class Day : INotifyPropertyChanged
    {
        private DateTime date;
        private bool isSelected;
        private string title;
        private List<Appointment> appointmentsList = new List<Appointment>();

        #region Constructor

        public Day(DateTime date, BaseCalendar parent)
        {
            this.Date = date;
            this.Parent = parent;
            this.Items = Enumerable.Range(0, 24).Select(item => new Hour(item, this));
            this.IsToday = date.Date == DateTime.Today;
            this.IsWeekEnd = date.Date.DayOfWeek == DayOfWeek.Saturday || date.Date.DayOfWeek == DayOfWeek.Sunday;
        }


        
        #endregion Constructor

        #region Properties

        public IEnumerable<Appointment> AllDayAppointments
        {
            get;
            private set;
        }

        public IEnumerable<Appointment> Appointments
        {
            get;
            private set;
        }

        public IEnumerable<Hour> Items
        {
            get;
            private set;
        }

        public DateTime Date
        {
            get
            {
                return date;
            }
            set
            {
                date = value;
                OnPropertyChanged(()=> Date);
            }
        }

        public bool IsSelected
        {
            get
            {
                return isSelected;
            }
            set
            {
                // If the item is already selected, de-selecet it
                if (isSelected == true)
                    value = false;
                isSelected = value;
                OnPropertyChanged(() => IsSelected);
                // remove all selected items
                Parent.RefreshSelectedDay();
            }
        }

        public bool IsToday
        {
            get;
            private set;
        }

        public bool IsWeekEnd
        {
            get;
            private set;
        }

        public BaseCalendar Parent
        {
            get;
            private set;
        }

        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
                OnPropertyChanged(() => Title);
            }
        }

        #endregion Properties
      
        internal void AddAppointment(string subjectText)
        {
            this.appointmentsList.Add(new Appointment()
            {
                AllDay = true,
                Subject = subjectText,
            });

            this.AllDayAppointments = this.appointmentsList.Where(item => item.AllDay);
            this.Appointments = appointmentsList.Where(item => item.AllDay == false);
            OnPropertyChanged(() => AllDayAppointments);
            OnPropertyChanged(() => Appointments);
        }

        private void OnPropertyChanged<T>(Expression<Func<T>> selectorExpression)
        {
            if (selectorExpression == null)
                throw new ArgumentNullException("selectorExpression");
            MemberExpression body = selectorExpression.Body as MemberExpression;
            if (body == null)
                throw new ArgumentException("The body must be a member expression");
            OnPropertyChanged(body.Member.Name);
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;


       

    }
}